package online.abor.day12_study.provider_consumer.baozi_plus;


import java.util.Random;

public class Zuo extends Thread {
    private final BaoZiBox baoZiBox;

    public Zuo(String name, BaoZiBox baoZiBox) {
        super(name);
        this.baoZiBox = baoZiBox;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (baoZiBox) {
                if (baoZiBox.getCount() >= 5) {
                    // 包子数量够了，就等着
                    try {
                        baoZiBox.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    Baozi baozi = Baozi.getInstance();
                    baoZiBox.getBaoziList().add(baozi);
                    System.out.println(Thread.currentThread().getName() + "做包子[" + baozi + "],还剩包子["+baoZiBox.getCount()+"]个");
                    // baoZiBox.notify();
                    baoZiBox.notifyAll();
                    try {
                        Thread.sleep(new Random().nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
